MySQL BETWEEN Operator

The BETWEEN operator is used to get the records between the given range.

The BETWEEN operator will have the start and the end values with which it will identify the range.

We shall use the following employee table to which we will query using the above operators.

empno name age role location salary
001 Andrew 30 Manager India 100000
002 Beslin 28 Business Analyst India 50000
003 Joanna 23 Senior Developer USA 500000
004 Rayan 26 Technical Lead Canada 500000

Syntax for BETWEEN Operator

select * from table_name
WHERE column_name BETWEEN value1 AND value2;

Example

select * from employee
where age BETWEEN 22 and 27;

Output

empno name age role location salary
003 Joanna 23 Senior Developer USA 500000

We can also use NOT BETWEEN operator to get all the records other than the matched condition.

Example

select * from employee
where age NOT BETWEEN 22 and 27;

Output

empno name age role location salary
001 Andrew 30 Manager India 100000
002 Beslin 28 Business Analyst India 50000
004 Rayan 26 Technical Lead Canada 500000

The BETWEEN operator is not restricted to number type. We can also check the text as well as date range with BETWEEN operator.

Consider the table employee with the joining_date column.

empno name age role location salary joining_date
001 Andrew 30 Manager India 100000 7/4/2020
002 Beslin 28 Business Analyst India 50000 7/5/2020
003 Joanna 23 Senior Developer USA 500000 7/6/2020
004 Rayan 26 Technical Lead Canada 500000 7/7/2020

Example

select * from employee 
where joining_date BETWEEN '2020-01-01' and '2020-06-30';

Output

empno name age role location salary joining_date
001 Andrew 30 Manager India 100000 7/4/2020
002 Beslin 28 Business Analyst India 50000 7/5/2020
003 Joanna 23 Senior Developer USA 500000 7/6/2020

Most Read